package com.magicsoft.share.ui.my;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.AlipayParam;
import com.magicsoft.share.bean.my.WxParameter;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.constant.Url;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.presenter.my.GoldNoVipPresenter;
import com.magicsoft.share.ui.main.H5Activity;
import com.magicsoft.share.utils.PayUtil;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.my.IGoldNoVipView;

import butterknife.BindView;
import butterknife.OnClick;

public class GoldNoVipActivity extends BaseActivity<IGoldNoVipView, GoldNoVipPresenter> implements IGoldNoVipView{


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.btn_become_vip)
    Button btnBecomeVip;
    @BindView(R.id.btn_rule)
    Button btnRule;
    @BindView(R.id.btn_explain)
    Button btnExplain;

    private static final int SDK_PAY_FLAG = 1;

    private AlertDialog edtDialog;
    private AlertDialog payDialog;
    public int payMethod;

    private String mShareId ;

    @Override
    public GoldNoVipPresenter initPresenter() {
        return new GoldNoVipPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_gold_no_vip;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void requestData() {

    }


    @OnClick({R.id.ibtn_left, R.id.btn_become_vip, R.id.btn_rule, R.id.btn_explain})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.btn_become_vip:
                Log.e("data","da");
                initEdtDialog();
                break;
            case R.id.btn_rule:
                Intent intent1 = new Intent(this, H5Activity.class);
                intent1.putExtra("urlStr", Url.BASE_URL+"api/open/platform/membershipRules");
                intent1.putExtra("titleStr", "会员规则");
                startActivity(intent1);
                break;
            case R.id.btn_explain:
                Intent intent2 = new Intent(this, H5Activity.class);
                intent2.putExtra("urlStr", Url.BASE_URL+"api/open/platform/invitationToNote");
                intent2.putExtra("titleStr", "邀请说明");
                startActivity(intent2);
                break;
        }
    }

    private void initEdtDialog() {
        View dialogEdtView = LayoutInflater.from(this).inflate(R.layout.dialog_edt_id, null);
        final EditText editText = (EditText) dialogEdtView.findViewById(R.id.edt);
        dialogEdtView.findViewById(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mShareId = editText.getText().toString().trim();
                if (edtDialog.isShowing()) {
                    edtDialog.dismiss();
                }
                mPresenter.getCheckQXCode(mShareId);
            }
        });

        edtDialog = new AlertDialog.Builder(this).setView(dialogEdtView)
                .show();
    }


    private void initPayDialog() {

        payMethod = Constant.PAY_WEICHAT ;

        View dialogPayView = LayoutInflater.from(this).inflate(R.layout.dialog_pay_select, null);

        final RadioButton rbtnPayWeChat = (RadioButton) dialogPayView.findViewById(R.id.rbtn_pay_wechat);
        final RadioButton rbtnPayZFB = (RadioButton) dialogPayView.findViewById(R.id.rbtn_pay_zfb);

        rbtnPayWeChat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    rbtnPayZFB.setChecked(false);
                    payMethod = Constant.PAY_WEICHAT;

                }
            }
        });
        rbtnPayZFB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    rbtnPayWeChat.setChecked(false);
                    payMethod = Constant.PAY_ZFB;
                }
            }
        });


        dialogPayView.findViewById(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (payMethod == Constant.PAY_ZFB){
                    mPresenter.getAlipayParam(mShareId);
                }else{
                    mPresenter.getWechatPayParam(mShareId);
                }

                if (payDialog.isShowing()) {
                    payDialog.dismiss();
                }

            }
        });

        payDialog = new AlertDialog.Builder(this).setView(dialogPayView)
                .show();
    }

    @Override
    public void getAlipayParamSuccess(AlipayParam alipayParam) {
        PayUtil.newInstance().alipay(this, alipayParam.getBody(), new PayUtil.AlipayCallBack() {
            @Override
            public void paySuccess() {
                ToastUtils.showToast(GoldNoVipActivity.this, "成功");
            }

            @Override
            public void payFail() {
                initFailDialog();
            }
        });
    }

    @Override
    public void getWechatPaySuccess(WxParameter wxParameter) {
        PayUtil.newInstance().wechatPay(this, wxParameter);
    }

    @Override
    public void checkQxcode(Boolean isSueccss) {
        if (isSueccss){
            initPayDialog();
        }else{
            ToastUtils.showToast(this,"该奇享号不存在!");
        }
    }

    private void initFailDialog() {
        new AlertView.Builder().setContext(this)
                .setStyle(AlertView.Style.Alert)
                .setTitle("支付失败")
                .setMessage(null)
                .setCancelText("确认")
                .setOthers(null)
                .build()
                .show();


    }
}
